package com.zj.juc.study.lockSupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName LockCoditionAwaitSignal
 * @Description Lock包下的Condition下的await和signal唤醒等待机制
 * @createTime 2024年12月26日 22:56
 */
public class LockCoditionAwaitSignal
{
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(()->{
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"\t....come in");
                condition.await();//阻塞，释放锁
                System.out.println(Thread.currentThread().getName()+"\t....被唤醒");
            }catch (Exception e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }
        },"t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"\t...唤醒开始");
            }catch (Exception exception){
                exception.printStackTrace();
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }
}
